package com.java.test.system.templatemember;

import com.java.test.controller.BaseController;
import com.java.test.dto.PageResultDTO;
import com.java.test.dto.ResponseDTO;
import com.java.test.system.dto.LoginAccountInfo;
import com.java.test.system.templatemember.dto.TemplateAddDTO;
import com.java.test.system.templatemember.dto.TemplateDTO;
import com.java.test.system.templatemember.dto.TemplateQueryDTO;
import com.java.test.system.templatemember.dto.TemplateUpdateDTO;
import com.java.test.system.templatemember.vo.TemplateVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * 模板管理Controller
 *
 * @author lijie1
 * @date 2020/6/30 0030
 */
@Slf4j
@RestController
@RequestMapping("/systemConfig/template")
public class TemplateController extends BaseController {

    TemplateService templateService;
    @Autowired
    public void setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
    }

    /**
     * 查询
     * @return
     */
    @RequestMapping(value = "/selectToList",method = RequestMethod.POST)
    public ResponseDTO<PageResultDTO<TemplateVO>> selectByList(@RequestBody @Valid TemplateQueryDTO queryDTO) {
        log.debug("----------模板列表查询----------");
        return templateService.getSelectByList(queryDTO);
    }

    /**
     * 新增
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ResponseDTO<String> save(@RequestBody @Valid TemplateAddDTO addDTO, HttpServletRequest request) {
        log.info("----------模板添加----------");
        addDTO.setStatus(1);
        addDTO.setCreateTime(new Date());
        LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
        systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "系统管理-模板设置-模板添加");
        addDTO.setOperation(user.getLoginAccout());
        return templateService.save(addDTO);
    }

    /**
     * 通过id获取数据
     * @param dto
     * @return
     */
    @RequestMapping(value = "/selectById",method = RequestMethod.POST)
    public ResponseDTO<TemplateVO> selectById(@RequestBody @Valid TemplateDTO dto) {
        log.info("----------模板查询----------");
        return templateService.selectById(dto.getId());
    }

    /**
     * 通过id修改数据
     * @param updateDTO
     * @return
     */
    @RequestMapping(value = "/updateById",method = RequestMethod.POST)
    public ResponseDTO<String> updateById(@RequestBody @Valid TemplateUpdateDTO updateDTO, HttpServletRequest request) {
        log.info("----------模板修改----------");
        updateDTO.setModifyTime(new Date());
        updateDTO.setStatus(1);
        LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
        systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "系统管理-模板设置-模板修改");
        return templateService.updateById(updateDTO);
    }

    /**
     * 通过id删除数据
     * @param dto
     * @return
     */
    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    public ResponseDTO<String> deleteById(@RequestBody @Valid TemplateDTO dto, HttpServletRequest request) {
        log.info("----------模板删除----------");
        LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");
        systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "系统管理-模板设置-模板删除");
        return templateService.deleteById(dto.getId());
    }
}
